Feature Group
bash internal / data manipulations (105)
9
bash
This code snippet measures the elapsed time of a script using Bash's built-in SECONDS
variable, simulating work with sleep 2
, and then calculates and displays the execution time.
start_time=$SECONDS # Simulate some work sleep 2 elapsed_time=$((SECONDS - start_time)) echo "Script took $elapsed_time seconds to run."
bash internaldata manipulationstime operationsmeasuring elapsed time
16
bash
This demonstrates using the IFS
variable to split a comma-separated string and iterate over its elements in a for
loop.
IFS=',' string="apple,banana,cherry" for fruit in $string; do echo "Fruit: $fruit" done
bash internaldata manipulationsstring manipulation and expansionsIFS
(Internal Field Separator)
19
bash
This demonstrates the use of built-in Bash variables to access script information like return values, process ID, and arguments.
echo "Last program's return value: $?" echo "Script's PID: $$" echo "Number of arguments passed to script: $#" echo "All arguments passed to script: $@" echo "Script's arguments separated into different variables: $1 $2..."
bash internaldata manipulationspositional argumentsbuilt-in variables